<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body{
      position: relative;
      width: 100vw;
      height: 10000px;
      margin: 0;
      background-image: linear-gradient(#e66465, #9198e5);
    }
    #mask{
      position: fixed;
      width: 100vw;
      height: 100vh;
      background-color: rgba(0, 0, 0, 0.6);
      top: 0;
      left: 0;
    }
    .modal{
      position: fixed;
      top: 0;
      left: 0;
      width: 100vw;
      height: 50vh;
      background: #fff;
      overflow: scroll;
    }
  </style>
</head>
<body>
  <div id="mask">
    <div class="modal">
      <h1>A</h1>
      <h1>B</h1>
      <h1>C</h1>
      <h1>D</h1>
      <h1>E</h1>
      <h1>F</h1>
      <h1>G</h1>
      <h1>H</h1>
      <h1>I</h1>
      <h1>G</h1>
      <h1>K</h1>
      <h1>L</h1>
      <h1>M</h1>
    </div>
  </div>
</body>
<script>
  var mask = document.querySelector('#mask');
  var modal = document.querySelector('.modal');

  // 记录初次touch纵坐标
  let startY = 0;

  const modalHeight = modal.clientHeight;
  const modalScrollHeight = modal.scrollHeight;

  modal.addEventListener("touchstart", (e) => {
    startY = e.touches[0].pageY;
  })

  mask.addEventListener("touchmove", (e) => {
    let endY = e.touches[0].pageY;
    let delta = endY - startY;

    if(
      (modal.scrollTop === 0 && delta > 0) ||
      (modal.scrollTop + modalHeight === modalScrollHeight &&
                    delta < 0)
    ){
      e.preventDefault()
    }
  }, true);
</script>
</html>

